import {WX_BINDPHONE, WX_CREATE_QRCODE, WX_LOGIN, WX_LOGOUT, WX_USER} from '../constants/wx'
import {API_CREATE_QRCODE, API_LOGIN, API_LOGOUT, API_BINDPHONE, API_USER} from '../constants/api'
import {createAction} from '../utils/redux'
import Taro from '@tarojs/taro'

/**
 * 获取二维码
 * @param {*} payload
 */
export const dispatchWxQrcode = (path) => createAction({
  url: API_CREATE_QRCODE,
  type: WX_CREATE_QRCODE,
  payload: {path},
  method: 'POST',
  cb: (result) => {
    console.log('result', result)
    // return 'data:image/png;base64,'+result
    return result
  }
})

/**
 * @module wx
 * @author: ymy
 * @description:登录
 * @since: 创建时间  2020-08-03 18:12:50
 */
export const dispatchWxLogin = (code, userInfo) => createAction({
  url: API_LOGIN,
  type: WX_LOGIN,
  payload: {code, ...userInfo},
  method: 'POST',
  cb: (result) => {
    // console.log(constant.host.substr(0,constant.host.length -6) + result)
    // return constant.host.substr(0,constant.host.length -6) + result
    // return 'data:image/png;base64,'+result
    Taro.setStorageSync('token', result)
    // const userInfo = Taro.getStorageSync('userInfo')
    Taro.showToast({
      title: '登录成功！',
      icon: 'success',
      duration: 2000
    })
    return result
  }
})

/**
 * @module wx
 * @author: ymy
 * @description:获取手机号
 * @since: 创建时间  2020-10-06 22:55:55
 */
export const dispatchWxBindPhone = (iv, encryptedData) => createAction({
  url: API_BINDPHONE,
  type: WX_BINDPHONE,
  payload: {iv, encryptedData},
  method: 'POST',
  cb: (result) => {
    Taro.setStorageSync('phone', result)
    Taro.showToast({
      title: '绑定成功！',
      icon: 'success',
      duration: 2000
    })
    return result
  }
})


/**
 * @module wx
 * @author: ymy
 * @description:登录
 * @since: 创建时间  2020-08-03 18:12:50
 */
export const dispatchWxLogout = (payload) => createAction({
  url: API_LOGOUT,
  type: WX_LOGOUT,
  payload,
  method: 'POST',
  cb: (result) => {
    return result
  }
})


/**
 * @module wx
 * @author: ymy
 * @description:用户信息
 * @since: 创建时间  2020-10-07 10:45:20
 */
export const dispatchWxUser = (payload) => createAction({
  url: API_USER,
  type: WX_USER,
  payload,
  method: 'GET',
  showToast: false,
  cb: (result) => {
    return result
  }
})
